//app.js

//全局组件


//工具
var util = require('/utils/util.js')

App({
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到微信后台换取 openId, sessionKey, unionId
        console.log(res,'res');

        this.globalData.wx_code = res.code;
       
      }
    })
    // 获取用户信息,已授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框，未授权的先授权
    var that = this;

    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo
              console.log('登录', this.globalData.userInfo);

              that.login();

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
    
  },
  config: {
    
    host: 'http://119.23.19.128:8080/lw_sfz',
    // host:'http://192.168.3.18:8080/lw_sfz',
    //版本
    version: "1.0.0",
    //app名称
    channel: '泉州丰泽旅游'
  },
  globalData: {
    //微信用户信息
    userInfo: null,
    //操作用户信息
    user: wx.getStorageSync('user') || null,
    token: wx.getStorageSync('token'),
   
    //微信用户唯一id
    openid: wx.getStorageSync('openid') || null,

    wx_code:'',

    binding_phone:false,//绑定手机号
    phone_num:'',//手机号

  },
  apiList:{
    do_login: '/weixin/api/auth/login',      // 登录
    hotnews_list: '/sfz/api/news/list', //新闻热点-新闻列表
    strategy: '/strategy',//攻略列表
    gift_list: '/sfz/api/gift/list', //礼物列表
    giftdetail: '/sfz/api/gift/detail', //礼物详情
    gift_review: '/sfz/api/gift/reviewList',//礼物评论列表
    scenedetail: '/scenedetail', //景区详情
    adList:'/sfz/api/wxTopImg/moduleTopImgList',//首页-广告列表
    travelPlan:'/travel/plane',//智能行程，行程预约计划
    userTravel_detail: '/sfz/api/userTravel/detail',//我的行程，用户行程详细信息
    
    travelDetail: '/travel/arrange',//智能行程，详细行程安排
    recommend_trip:'/sfz/api/travel/list',//推荐行程
    travel_detail:'/sfz/api/travel/detail',//推荐行程详细信息
    travelDayList: '/sfz/api/travel/travelDayList',//推荐行程天列表数据
    travelDaySiteMapList: '/sfz/api/travel/travelDaySiteMapList',//推荐行程天地点地图列表数据
    travel_travelDaySiteList: '/sfz/api/travel/travelDaySiteList', //推荐行程天地点列表数据
    industryTypeList: '/sfz/api/industry/industryTypeList',//行业列表数据
    siteListByIndustryType: '/sfz/api/siteInfo/siteListByIndustryType',//根据行业类型获取地点列表数据
    updateUserTravel: '/sfz/api/userTravel/updateUserTravel',//新增用户行程
    updateUserTravel: '/sfz/api/userTravel/updateUserTravel',//更新用户行程
    fromCollectAddTravel: '/sfz/api/userTravel/fromCollectAddTravel',//从收藏中增加用户行程

    userTravel_detail: '/sfz/api/userTravel/detail',//用户行程详细信息
    userTravel_travelDayList: '/sfz/api/userTravel/travelDayList',//用户行程天列表数据
    userTravel_travelDaySiteList: '/sfz/api/userTravel/travelDaySiteList',//用户行程天地点列表数据
    userTravel_travelDaySiteMapList: '/sfz/api/userTravel/travelDaySiteMapList',//用户行程天地点地图列表数据
    userTravel_fullDetail: '/sfz/api/userTravel/fullDetail',//用户行程完整详细信息

    addCollectTravel: '/sfz/api/userTravel/addCollectTravel', //收藏行程
    delCollectTravel:'/sfz/api/userTravel/delCollectTravel',//取消收藏
    userTravel: '/sfz/api/userTravel/userList',//我的行程-用户行程列表数据
    collectTravelList: '/sfz/api/userTravel/collectTravelList',//用户收藏行程列表数据

    upload_img: '', //上传图片
    meishiList:'/sfz/api/cate/list',//美食列表
    meishi_storeInfo:'/sfz/api/cate/detailInfo',//美食商户详情
    meishi_comment_list:'/sfz/api/siteReview/list',//...评论列表
    meishi_recommend_list:'/sfz/api/cate/cookbookList',//美食菜单推荐列表
    meishi_grouppurchase_list:'/meishi/groupPurchase',//团购

    shop_list:'/sfz/api/shop/list',//商圈列表
    shop_circle_info:'/sfz/api/shop/commercialDistrictDetail',//商圈信息
    shops:'/sfz/api/shop/storeList',//商铺列表
    storeDetail: '/sfz/api/shop/storeDetail', //商铺详细信息
    goodsList:'/sfz/api/shop/goodsList',//商铺--商品列表
    hotel_list:'/sfz/api/hotel/list',//酒店列表
    hotel_info:'/sfz/api/hotel/detailInfo',//酒店详情
    hotel_facilityList:'/sfz/api/hotel/hotelFacilityList',//酒店设施
    hotelroom_list: '/sfz/api/hotelRoom/listByHotelId',//房间列表数据
    room_info:'/sfz/api/hotelRoom/detailInfo',//酒店-房间信息
    hotel_rules:'/sfz/api/hotel/hotelRoomRuleList',//酒店规则

    scenic_list:'/sfz/api/scenic/list',//游，景点列表
    scenic_info: '/sfz/api/scenic/scenicDetail',//景点详细信息
   

    collection_list:'/collec/colleclist',//行程收藏列表

    enjoy_list:'/sfz/api/recreation/list', //娱乐列表
    enjoy_detail:'/sfz/api/recreation/detailInfo',//娱乐详情

    giftShop_list: '/sfz/api/gift/giftShopList',//礼物关联商铺列表数据

    bind_phone:'/weixin/api/auth/register',//绑定手机号
    

  },
  apiGet: function (url, data, callback, title) {
    var that = this;
    title === 'undefined' ? this.loading() : this.loading(title);
    wx.request({
      url: this.config.host + url,
      data: data,
      method: 'GET',
      dataType: 'json',
      header: { 
        'content-type': 'application/json;charset=UTF-8',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        callback(res.data)
      },
      fail: function (res) {
        console.log(url + '请求失败')
      },
      complete: function (res) {
        console.log(url + '请求完成')
        console.log(res);
        that.hideloading();
      }
    })
  },
  apiPost: function (url, data, callback, title) {
    var that = this;
    title === 'undefined' ? this.loading() : this.loading(title);
    this.loading(title);
    wx.request({
      url: this.config.host + url,
      data: data,
      method: 'POST',
      dataType: 'json',
      header: { 
        "content-type": "application/json",
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        callback(res.data)
      },
      fail: function (res) {
        console.log(url + '请求失败')
        that.alert('请求失败，请重试');
      },
      complete: function (res) {
        console.log(url + '请求完成')
        that.hideloading();
        console.log(res);
      }
    })
  },
  /*
  *获取当前位置的经纬度
  */
  getLocation: function (callback){
    wx.getLocation({
      type: 'wgs84',
      success: (res) => {
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy;

        var obj = {
          latitude: latitude,
          longitude: longitude,
          speed: speed,
          accuracy: accuracy
        }
        console.log(obj,'当前坐标');
        callback(obj);
        // this.setData({ latitude: latitude, longitude: longitude })
        // wx.showModal({
        //   title: '当前位置',
        //   content: '经度' + res.longitude + '纬度' + res.latitude,
        // })
      }

    })
  },
  alert: function (msg) {
    wx.showModal({
      content: msg,
      showCancel: false,
    });
  },
  loading: function (title = '玩命加载中...') {
    wx.showLoading({
      title: title,
      mask: true,

    });
  },
  hideloading: function () {
    wx.hideLoading();
  },
  login: function () {
    var that = this;
    that.apiPost(that.apiList.do_login, {
      content: {
        wxCode: that.globalData.wx_code,
        nickname: that.globalData.userInfo.nickName,
        avatarurl: that.globalData.userInfo.avatarUrl,
        appId: 'wx3434b64fab872bfb'
      }
    }, function (res) {
      console.log('每次都给后台请求token', res, res.token);
      wx.setStorageSync('token', res.token);

    })
  },
  util
})
